import java.io.File;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class ArrayIdAuthor {

	/**
	 * @param args
	 */
	public ArrayList<String> GetIdAuthor() {
        // TODO code application logic here
        ArrayList<String> karaAuthorList = new ArrayList<String>();
        try {
			String fileName = "E:\\Nam4_HK2\\Java\\DoAnCuoiKi\\file.xml";
			File fXMLFile = new File(fileName);
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(fXMLFile);
			doc.getDocumentElement().normalize();
 
			NodeList nList = doc.getElementsByTagName("song");
	 	 
			for (int temp = 0; temp < nList.getLength(); temp++) {
	 
			Node nNode = nList.item(temp);
	 
			String tempString = "";
	       	String tempId;
	        String tempAuthor = "";
	      	String tempExtra = "";
			if (nNode.getNodeType() == Node.ELEMENT_NODE) {	                        
				Element eElement = (Element) nNode;
	                        tempId = "[" + eElement.getElementsByTagName("id").item(0).getTextContent() + "]";
	                        tempAuthor = eElement.getElementsByTagName("author").item(0).getTextContent();
	                        if(!"Nhạc: ".equals(tempAuthor.substring(0,6)))
	                            tempAuthor = "[" + eElement.getElementsByTagName("author").item(0).getTextContent() + "]";
	                        else
	                        {
	                            tempAuthor = tempAuthor.substring(6, tempAuthor.length());
	                            int infoTho = tempAuthor.indexOf(" Thơ: ");
	                            int infoLoi = tempAuthor.indexOf(" Lời: ");
	                            
	                            if(infoTho != -1)
	                            {
	                                tempExtra = tempAuthor.substring(infoTho + 6, tempAuthor.length());
	                                tempAuthor = tempAuthor.substring(0, infoTho);
	                                tempAuthor = "["+ tempAuthor + "][" + tempExtra+ "]";
	                            }                                                 
	                            if(infoLoi != -1)
	                            {
	                                tempExtra = tempAuthor.substring(infoLoi + 6, tempAuthor.length());
	                                tempAuthor = tempAuthor.substring(0, infoLoi);
	                                tempAuthor = "["+ tempAuthor + "][" + tempExtra+ "]";
	                            }                         
	                        }
	                        
	                        tempString+= tempId + tempAuthor;
	                        karaAuthorList.add(tempString);
			}
		}
	    } catch (Exception e) {
	    	e.printStackTrace();
	    }
		return karaAuthorList;
    }

}
